package com.bankcomm.ui.specialwidget;

import com.bankcomm.R;
import com.bankcomm.common.LPMid;
import com.bankcomm.ui.BaseViewCopy;
import com.bankcomm.ui.specialwidget.LPDigitPasswordTextField.MyLPDigitPasswordTextField;
import com.rytong.tools.utils.Utils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Vibrator;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


/**
 * 系统定制的数字密码键盘
 * 
 */
public class DigitPasswordKeyPad extends View {
	BaseViewCopy bv_;
	EditText edit_;
	public Dialog dlg_;

	// 最大可输入长度
	int length = 6;

	private EditText digitkeypad_edittext;
	private Button digitkeypad_0;
	private Button digitkeypad_1;
	private Button digitkeypad_2;
	private Button digitkeypad_3;
	private Button digitkeypad_4;
	private Button digitkeypad_5;
	private Button digitkeypad_6;
	private Button digitkeypad_7;
	private Button digitkeypad_8;
	private Button digitkeypad_9;
	private Button digitkeypad_clear;
	private Button digitkeypad_ok;

	private String[] resultNum;
	private String[] resultLetter;
	private StringBuffer numBuffer_;
	private StringBuffer letterBuffer_;
	private LPMid mid_;
	private Vibrator vibrator ;

	public DigitPasswordKeyPad(Context ctx, MyLPDigitPasswordTextField edit) {
		super(ctx);
		bv_ = (BaseViewCopy) ctx;
		edit_ = (EditText) edit;
		mid_=LPMid.getInstance();
	}
	
	public DigitPasswordKeyPad(Context ctx, EditText edit) {
		super(ctx);
		bv_ = (BaseViewCopy) ctx;
		edit_ = edit;
		mid_=LPMid.getInstance();
	}

	public View setup() {
		vibrator = (Vibrator)bv_.vibrator(); //调用系统震动
		resultNum = mid_.num;
		resultLetter = mid_.letter;
		
		numBuffer_ = new StringBuffer();
		letterBuffer_ = new StringBuffer();

		LayoutInflater lif = LayoutInflater.from(bv_);
		View v = lif.inflate(R.layout.digitpasswordkeypad, null);

		// 初始化 对象
		digitkeypad_edittext = (EditText) v.findViewById(R.id.digitpadedittext);
		digitkeypad_edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
		digitkeypad_edittext.setBackgroundColor(Color.WHITE);
		digitkeypad_edittext.setFocusable(false);
		
		digitkeypad_0 = (Button) v.findViewById(R.id.digitkeypad_0);
		digitkeypad_0.setText(resultNum[0]);

		digitkeypad_1 = (Button) v.findViewById(R.id.digitkeypad_1);
		digitkeypad_1.setText(resultNum[1]);

		digitkeypad_2 = (Button) v.findViewById(R.id.digitkeypad_2);
		digitkeypad_2.setText(resultNum[2]);

		digitkeypad_3 = (Button) v.findViewById(R.id.digitkeypad_3);
		digitkeypad_3.setText(resultNum[3]);

		digitkeypad_4 = (Button) v.findViewById(R.id.digitkeypad_4);
		digitkeypad_4.setText(resultNum[4]);

		digitkeypad_5 = (Button) v.findViewById(R.id.digitkeypad_5);
		digitkeypad_5.setText(resultNum[5]);

		digitkeypad_6 = (Button) v.findViewById(R.id.digitkeypad_6);
		digitkeypad_6.setText(resultNum[6]);

		digitkeypad_7 = (Button) v.findViewById(R.id.digitkeypad_7);
		digitkeypad_7.setText(resultNum[7]);

		digitkeypad_8 = (Button) v.findViewById(R.id.digitkeypad_8);
		digitkeypad_8.setText(resultNum[8]);

		digitkeypad_9 = (Button) v.findViewById(R.id.digitkeypad_9);
		digitkeypad_9.setText(resultNum[9]);

		digitkeypad_clear = (Button) v.findViewById(R.id.digitkeypad_clear);
		digitkeypad_ok = (Button) v.findViewById(R.id.digitkeypad_ok);

		// 添加点击事件
		DigitPasswordKeypadOnClickListener dkol = new DigitPasswordKeypadOnClickListener();
		digitkeypad_0.setOnClickListener(dkol);
		digitkeypad_1.setOnClickListener(dkol);
		digitkeypad_2.setOnClickListener(dkol);
		digitkeypad_3.setOnClickListener(dkol);
		digitkeypad_4.setOnClickListener(dkol);
		digitkeypad_5.setOnClickListener(dkol);
		digitkeypad_6.setOnClickListener(dkol);
		digitkeypad_7.setOnClickListener(dkol);
		digitkeypad_8.setOnClickListener(dkol);
		digitkeypad_9.setOnClickListener(dkol);
		digitkeypad_clear.setOnClickListener(dkol);
		digitkeypad_ok.setOnClickListener(new DigitPasswordKeypadFinshOnClikcListener());

		return v;
	}

	private class DigitPasswordKeypadOnClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			int viewId = v.getId();

			switch (viewId) {
			case R.id.digitkeypad_0:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[0]);
					letterBuffer_.append(resultLetter[0]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_1:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[1]);
					letterBuffer_.append(resultLetter[1]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_2:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[2]);
					letterBuffer_.append(resultLetter[2]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_3:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[3]);
					letterBuffer_.append(resultLetter[3]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_4:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[4]);
					letterBuffer_.append(resultLetter[4]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_5:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[5]);
					letterBuffer_.append(resultLetter[5]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_6:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[6]);
					letterBuffer_.append(resultLetter[6]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_7:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[7]);
					letterBuffer_.append(resultLetter[7]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_8:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[8]);
					letterBuffer_.append(resultLetter[8]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_9:
				if (numBuffer_.length() == length) {
					return;
				} else {
					numBuffer_.append(resultNum[9]);
					letterBuffer_.append(resultLetter[9]);
					vibrator.vibrate(50);
				}
				break;
			case R.id.digitkeypad_clear:
				if (numBuffer_.length() > 0) {
					int len = digitkeypad_edittext.getSelectionEnd();
					numBuffer_ = numBuffer_.deleteCharAt(len - 1);
					letterBuffer_ = letterBuffer_.deleteCharAt(len - 1);
				}
				break;
			}
			// 格式化 数据
			digitkeypad_edittext.setText(numBuffer_.toString());
			digitkeypad_edittext.setSelection(null != numBuffer_ ? numBuffer_.length() : 0);
			
			edit_.setText(letterBuffer_.toString());
			edit_.setSelection(null != letterBuffer_ ? letterBuffer_.length() : 0);
		}
	}

	private class DigitPasswordKeypadFinshOnClikcListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			int viewId = v.getId();

			if (viewId == R.id.digitkeypad_ok) {
				// Log.i("数字：",  numBuffer_.toString());
				// Log.i("对应码：", letterBuffer_.toString());
				// edit_.setText(letterBuffer_.toString());
				dlg_.dismiss();
			}
		}
	}

}